package com.gitee.absolute_spirit.hiot_android.ui.light

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Card
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.gitee.absolute_spirit.hiot_android.R
import com.gitee.absolute_spirit.hiot_android.ui.theme.AppTheme

@Composable
fun SwitchView(
    switchViewModel: SwitchViewModel = hiltViewModel()
) {
    val uiState by switchViewModel.uiState.collectAsState()

    SwitchViewContent(uiState) {
        switchViewModel.changeOnOffState()
    }
}

@Composable
fun SwitchViewContent(
    uiState: SwitchViewState,
    powerButtonClick: () -> Unit,
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        Card(
            modifier = Modifier.fillMaxWidth(),
        ) {
            Surface(
                modifier = Modifier.fillMaxWidth(),
                color = if (uiState.on) MaterialTheme.colorScheme.secondary else MaterialTheme.colorScheme.surfaceVariant
            ) {
                Row(
                    modifier = Modifier.padding(16.dp),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    ElevatedButton(
                        onClick = powerButtonClick,

                    ) {
                        Icon(
                            painter = painterResource(id = R.drawable.baseline_power_settings_new_24),
                            contentDescription = "switch"
                        )
                    }
                }
            }
        }
    }

}


@Preview(showBackground = true)
@Composable
private fun SwitchViewPreview() {
    AppTheme {
        SwitchViewContent(SwitchViewState(on = true), {})
    }
}